﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using AbstractFramework.Display;
using AbstractFramework.LogicControl;
using AbstractFramework.Constant;

namespace ControlLib
{
	/// <summary>
	/// FloorButtonPanel.xaml 的交互逻辑
    /// 电梯内部按钮
	/// </summary>
    public partial class InsideButtonPanelDisplay : UserControl, IInsideButtonPanelDisplay
	{
        private int buttonNum;//按钮面板上的按钮个数，也就是楼层个数
        private List<Button> buttonList = new List<Button>();
        private IButtonFactory buttonFactory;
        private IElevator logicalElevator;
        int elevatorId;
        int[] fbdFloors;
		public InsideButtonPanelDisplay()
		{
			this.InitializeComponent();
		}

        public void initialize(int buttonNum,IElevator logicalElevator, IButtonFactory buttonFactory,int elevatorId,int[] fbdFloors)
        {
            if (buttonNum <= 0)
                this.buttonNum = 0;
            else
                this.buttonNum = buttonNum;
            this.buttonFactory = buttonFactory;
            this.elevatorId = elevatorId;
            this.fbdFloors = fbdFloors;
            this.logicalElevator = logicalElevator;
        }

        public void light(int buttonID)//亮灯
        {
            buttonList[buttonID].Background = new SolidColorBrush(Color.FromRgb(0xDF, 0xF3, 0xA9));
        }

        public void extinguish(int buttonID)//熄灯
        {
            buttonList[buttonID].Background = new SolidColorBrush(Color.FromRgb(0x73, 0x7D, 0x76));
        }

        public void setEnabled(bool isEnabled)
        {
            foreach (Button btn in buttonList)
            {
                btn.IsEnabled = isEnabled;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < buttonNum; i++)
            {
                Button newbtn = new Button();
                newbtn.Content = String.Format("{0}", i + 1);
                newbtn.Height = 20;
                newbtn.Width = 20;
                newbtn.Margin = new Thickness(2, 2, 2, 2);
                newbtn.Background = new SolidColorBrush(Color.FromRgb(0x73, 0x7D, 0x76));
                pnlAll.Children.Add(newbtn);
                buttonList.Add(newbtn);
                newbtn.Click += new RoutedEventHandler(newbtn_Click);
                if(ArrayContains(fbdFloors,i+1))
                {
                    newbtn.IsEnabled = false;
                }
            }

            Button openbtn = new Button();
            openbtn.Content = "< >";
            openbtn.Height = 20;
            openbtn.Width = 20;
            openbtn.Margin = new Thickness(2, 2, 2, 2);
            openbtn.Background = new SolidColorBrush(Color.FromRgb(0x73, 0x7D, 0x76));
            pnlAll.Children.Add(openbtn);
            buttonList.Add(openbtn);
            openbtn.Click += new RoutedEventHandler(openbtn_Click);

            Button closeBtn = new Button();
            closeBtn.Content = "> <";
            closeBtn.Height = 20;
            closeBtn.Width = 20;
            closeBtn.Margin = new Thickness(2, 2, 2, 2);
            closeBtn.Background = new SolidColorBrush(Color.FromRgb(0x73, 0x7D, 0x76));
            pnlAll.Children.Add(closeBtn);
            buttonList.Add(closeBtn);
            closeBtn.Click += new RoutedEventHandler(closeBtn_Click);

            this.Height = ((buttonNum+2) / 4 + 1) * 25;
        }

        void openbtn_Click(object sender, RoutedEventArgs e)
        {
            logicalElevator.Elevator_Display.open(false);
        }

        void closeBtn_Click(object sender, RoutedEventArgs e)
        {
            logicalElevator.Elevator_Display.close(false);
        }


        private bool ArrayContains(int[] fbdFloors, int e)
        {
            for(int i=0;i<fbdFloors.Length;i++)
            {
                if(fbdFloors[i]==e)
                {
                    return true;
                }
            }
            return false;
        }

        //通知事件模块，右按钮按下了
        void newbtn_Click(object sender, RoutedEventArgs e)
        {
            int clickedButtonId = buttonList.IndexOf((Button)sender);
            IInsideButton button = (IInsideButton)this.buttonFactory.createButton(ButtonType.insideButton);
            button.DestinationFloor = clickedButtonId;
            button.ElevatorNo = this.elevatorId;
            button.createButtonRequisition();

            //自己把自己点亮
            this.light(clickedButtonId);
        }
    }
}